package com.xiaoowuu.wherecanigo

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.fragment.app.DialogFragment
import kotlinx.android.synthetic.main.dialog_setting.*


/**
 * @author xiaoowuu
 */
class SettingDialog : DialogFragment(), SeekBar.OnSeekBarChangeListener {
    companion object {
        fun newInstance(distance: Int, stationCount: Int): SettingDialog {
            val dialog = SettingDialog()
            val arguments = Bundle()
            arguments.putInt("distance", distance)
            arguments.putInt("stationCount", stationCount)
            dialog.arguments = arguments
            return dialog
        }
    }

    private val stationCount: Int by lazy {
        if (arguments != null) {
            arguments!!.getInt("stationCount")
        } else {
            0
        }
    }
    private val distance: Int by lazy {
        if (arguments != null) {
            arguments!!.getInt("distance")
        } else {
            0
        }
    }
    var onSettingChangedListener: OnSettingChangedListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.AppTheme)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog.setCanceledOnTouchOutside(true)
        dialog.window?.let {
            it.setGravity(Gravity.CENTER)
            it.setBackgroundDrawableResource(android.R.color.transparent)
        }
        return inflater.inflate(R.layout.dialog_setting, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        sbNearByDistance.progress = distance
        tvNearByDistanceValue.text = sbNearByDistance.progress.toString()
        sbNearByDistance.setOnSeekBarChangeListener(this)

        sbStationCount.progress = stationCount
        tvStationCountValue.text = sbStationCount.progress.toString()
        sbStationCount.setOnSeekBarChangeListener(this)

        view.setOnClickListener { dismiss() }
        btnConfirm.setOnClickListener {
            dismiss()
            onSettingChangedListener?.onSettingChanged(sbNearByDistance.progress / 100 * 100, sbStationCount.progress)
        }
    }

    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        when (seekBar) {
            sbNearByDistance -> tvNearByDistanceValue.text = (progress / 100 * 100).toString()
            sbStationCount -> tvStationCountValue.text = progress.toString()
        }
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) {
    }

    override fun onStopTrackingTouch(seekBar: SeekBar?) {
    }

    interface OnSettingChangedListener {
        fun onSettingChanged(distance: Int, stationCount: Int)
    }
}